#include "EffectGenerator.h"
#include <fstream>
#include <streambuf>
#include "EngineFeatures.h"
#include "Effect.h"
#include "EffectSections.h"


EffectGenerator::EffectGenerator(void)
{

}


EffectGenerator::~EffectGenerator(void)
{

}

EffectGenerator * EffectGenerator::ptr = nullptr;

void EffectGenerator::Init()
{
	std::ifstream t("templateVS.txt");
	std::string strVS((std::istreambuf_iterator<char>(t)),
		std::istreambuf_iterator<char>());

	tempateVS = strVS;


	std::ifstream t("templateFS.txt");
	std::string strFS((std::istreambuf_iterator<char>(t)),
		std::istreambuf_iterator<char>());

	templateFS = strFS;
}

EffectGenerator * EffectGenerator::GetPtr()
{
	if(ptr == nullptr)
	{
		ptr = new EffectGenerator();
		ptr->Init();
	}

	return ptr;
}

std::string EffectGenerator::GenerateDefinesFromMask(int effectMask)
{
	std::string defines = "";


	if(effectMask & LIGHTING == LIGHTING)
	{
		defines += "#define LIGHTING \n";
	}

	if(effectMask & DIFFUSE_TEXTUREING == DIFFUSE_TEXTUREING)
	{
		defines += "#define DIFFUSE_TEXTURING \n";
	}

	if(effectMask & BUMP_MAPPING == BUMP_MAPPING)
	{
		defines += "#define BUMP_MAPPING \n";
	}
	if(effectMask & SHADOW_MAPPING == SHADOW_MAPPING)
	{
		defines += "#define SHADOW_MAPPING \n";
	}

	if(effectMask & SKINNING == SKINNING)
	{
		defines += "#define SKINNING \n";
	}

	if(effectMask & LIGHT_MAPPING == LIGHT_MAPPING)
	{
		defines += "#define LIGHT_MAPPING \n";
	}

	return defines;
}

void EffectGenerator::InitSections(Effect * effect,int effectMask)
{
		
	if(effectMask & LIGHTING == LIGHTING)
	{
		// to do
	}

	if(effectMask & DIFFUSE_TEXTUREING == DIFFUSE_TEXTUREING)
	{
		effect->AddSection(DIFFUSE_TEXTUREING,new DiffuseTexturingSection());
	}

	if(effectMask & BUMP_MAPPING == BUMP_MAPPING)
	{
		// to do
	}
	if(effectMask & SHADOW_MAPPING == SHADOW_MAPPING)
	{
		// to do
	}

	if(effectMask & SKINNING == SKINNING)
	{
		// to do
	}

	if(effectMask & LIGHT_MAPPING == LIGHT_MAPPING)
	{
		// to do
	}

}


Effect * EffectGenerator::GenerateEffect(int effectMask)
{
	Effect * eff = new Effect();
	std::string preDefs = GenerateDefinesFromMask(effectMask);
	EffectData data;
	data.vs = tempateVS;
	data.fsPP = templateFS;

	data.vsPP = preDefs;
	data.fsPP = preDefs;

	eff->Init(data);
	InitSections(eff,effectMask);

	return eff;
}